﻿Imports TBCC.UtilityMobile
Public Class CarHDService

    Public Shared Function GetHDCarList(ByVal parentId As String, ByVal projectId As String, ByVal startTime As String, ByVal endTime As String, ByVal timeValue As String) As IList(Of ProjectInfo.CarHDItem)
        Dim carHDList As IList(Of ProjectInfo.CarHDItem)
        carHDList = New List(Of ProjectInfo.CarHDItem)

        Dim dt As DataSet
        Dim strsql As String = ""
        'strsql += String.Format("select * from tbcchistdata_car_{0}_1 where parentId={3} and UpdateTime between '{1}' and '{2}' and ((HOUR(updatetime)*3600+ MINUTE(updatetime)*60+SECOND(updatetime))- (HOUR('{1}')*3600+ MINUTE('{1}')*60+SECOND('{1}'))) % {4} =0", projectId, startTime, endTime, parentId, timeValue)
        'strsql += String.Format("select * from tbcchistdata_car_{0}_1 where parentId={3} and UpdateTime between '{1}' and '{2}' and ((HOUR('{2}')*3600+ MINUTE('{2}')*60+SECOND('{2}'))- (HOUR('{1}')*3600+ MINUTE('{1}')*60+SECOND('{1}'))) % {4} =0", projectId, startTime, endTime, parentId, timeValue)



        'strsql += String.Format("select * from tbcchistdata_car_{0}_1 where parentId={3} and ((HOUR(updatetime)*3600+ MINUTE(updatetime)*60+SECOND(updatetime))- (HOUR('{1}')*3600+ MINUTE('{1}')*60+SECOND('{1}'))) % {4} =0", projectId, startTime, endTime, parentId, timeValue)

        strsql += String.Format("select * from tbcchistdata_Box_{0}_1 where parentId={3}", projectId, startTime, endTime, parentId, timeValue)

        dt = SqlHelper.ExecuteDataset(strsql, Nothing)

        For Each r As DataRow In dt.Tables(0).Rows
            Dim car As New ProjectInfo.CarHDItem

            car.ParentId = SqlHelper.GetDbData(r("ParentId"))
            car.UpdateTime = SqlHelper.GetDbData(r("UpdateTime"))
            car.Latitude_dir = SqlHelper.GetDbData(r("Latitude_dir"))
            'car.Latitude = SqlHelper.GetDbData(r("Latitude"))
            car.Latitude = "----"

            car.Longitude_dir = SqlHelper.GetDbData(r("Longitude_dir"))
            car.Longitude = SqlHelper.GetDbData(r("Longitude"))
            car.Longitude = "----"
            Dim count As Integer = car.Latitude.ToString.Length

            Dim str As String

            If car.Latitude_dir = 1 Then
                str = "'N"
            ElseIf car.Latitude_dir = 0 Then
                str = "'S"
            Else
                str = ""
            End If
            If count > 0 Then
              
                car.AlarmStatus = SqlHelper.GetDbData(r("AlarmStatus"))

                car.AI1 = SqlHelper.GetDbData(r("AI1"))
                car.AI2 = SqlHelper.GetDbData(r("AI2"))
                car.AI3 = SqlHelper.GetDbData(r("AI3"))
                car.AI4 = SqlHelper.GetDbData(r("AI4"))

                Select Case MainForm.readConfig("SystemType")
                    Case 0, 1
                    Case 2  '类型为2修改所有数据为合格数据
                        Select Case MainForm.readConfig("Type")
                            Case 0

                                '修改湿度数据为合格数据

                                If car.AI1 > 8 Then

                                    car.AI1 = 8 - 1 + car.AI1.Remove(0, car.AI1.LastIndexOf(".")) '+数据小数点儿'+数据小数点儿

                                ElseIf car.AI1 < 2 Then

                                    car.AI1 = 2 + car.AI1.Remove(0, car.AI1.LastIndexOf(".")) '+数据小数点儿'+数据小数点儿
                                End If

                            Case Else

                        End Select
                    Case Else

                End Select

            End If

            car.TransportState = SqlHelper.GetDbData(r("TransportState"))
            '  car.AlarmStatus = SqlHelper.GetDbData(r("histAlarmData"))



            carHDList.Add(car)




        Next
        Return carHDList
    End Function
    Public Shared Function UpdateCARFDAPState(ByVal pi As String, ByVal update As DateTime) As Boolean

        Dim strSql As String
        Dim tic As New TBCC.UtilityMobile.TranstrationItem
        strSql = String.Format("update  TbccHistData_Box_{0}_1 set Fdapfinished=?p0 where  UpdateTime=?p1", pi)

        Dim p(1) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.GetUpperBound(0)
            p(i) = New MySql.Data.MySqlClient.MySqlParameter
            p(i).ParameterName = "p" + i.ToString
        Next
        p(0).Value = 1
        p(1).Value = update

        tic.Add(strSql, p)

        Return SqlHelper.ExecuteTranstration(tic)

    End Function
    Public Shared Function GetFDAPCarList(ByVal projectId As String) As IList(Of ProjectInfo.CarHDItem)

        Dim carHDList As IList(Of ProjectInfo.CarHDItem)
        carHDList = New List(Of ProjectInfo.CarHDItem)

        Dim dt As DataSet
        Dim strsql As String = ""
        strsql = String.Format("select * from tbcchistdata_Box_{0}_1 where  fdapfinished={1} order by UpdateTime limit 10", projectId, 0)
        dt = SqlHelper.ExecuteDataset(strsql, Nothing)

        For Each r As DataRow In dt.Tables(0).Rows

            Dim car As New ProjectInfo.CarHDItem


            car.ParentId = SqlHelper.GetDbData(r("ParentId"))
            car.UpdateTime = SqlHelper.GetDbData(r("UpdateTime"))
            car.Latitude_dir = SqlHelper.GetDbData(r("Latitude_dir"))
            car.Latitude = SqlHelper.GetDbData(r("Latitude"))
            car.Longitude_dir = SqlHelper.GetDbData(r("Longitude_dir"))
            car.Longitude = SqlHelper.GetDbData(r("Longitude"))

            car.AlarmStatus = SqlHelper.GetDbData(r("AlarmStatus"))

            car.AI1 = SqlHelper.GetDbData(r("AI1"))
            car.AI2 = SqlHelper.GetDbData(r("AI2"))
            car.AI3 = SqlHelper.GetDbData(r("AI3"))
            car.AI4 = SqlHelper.GetDbData(r("AI4"))

            carHDList.Add(car)

        Next



        Return carHDList
    End Function
    Public Shared Function GetFDAPHDCarList(ByVal parentId As String, ByVal projectId As String) As IList(Of ProjectInfo.CarHDItem)

        Dim carHDList As IList(Of ProjectInfo.CarHDItem)
        carHDList = New List(Of ProjectInfo.CarHDItem)

        Dim dt As DataSet
        Dim strsql As String = ""
        strsql += String.Format("select * from tbcchistdata_Box_{0}_1 where parentId={1} and fdapfinished={2} limit 10", projectId, parentId, 0)
        dt = SqlHelper.ExecuteDataset(strsql, Nothing)

        For Each r As DataRow In dt.Tables(0).Rows

            Dim car As New ProjectInfo.CarHDItem


            car.ParentId = SqlHelper.GetDbData(r("ParentId"))
            car.UpdateTime = SqlHelper.GetDbData(r("UpdateTime"))
            car.Latitude_dir = SqlHelper.GetDbData(r("Latitude_dir"))
            car.Latitude = SqlHelper.GetDbData(r("Latitude"))
            car.Longitude_dir = SqlHelper.GetDbData(r("Longitude_dir"))
            car.Longitude = SqlHelper.GetDbData(r("Longitude"))

            car.AlarmStatus = SqlHelper.GetDbData(r("AlarmStatus"))

            car.AI1 = SqlHelper.GetDbData(r("AI1"))
            car.AI2 = SqlHelper.GetDbData(r("AI2"))
            car.AI3 = SqlHelper.GetDbData(r("AI3"))
            car.AI4 = SqlHelper.GetDbData(r("AI4"))

            carHDList.Add(car)

        Next



        Return carHDList
    End Function
    ''' <summary>
    ''' 查找数据中最后一条
    ''' </summary>
    ''' <param name="carPrp"></param>
    ''' <param name="pid"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetLastData(ByVal carPrp As String, ByVal pid As Integer) As DateTime
        Dim strSql As String = ""
        strSql += String.Format("select max(UpdateTime) from TbccHistData_Box_{0}_1 where ParentId={1} ", carPrp, pid)
        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return DateTime.MinValue
        End If

        Return obj

    End Function

    ''' <summary>
    ''' 上传对应启停记录数据
    ''' </summary>
    ''' <param name="pi"></param>
    ''' <param name="StartStopid"></param>
    ''' <param name="carHD"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>

    Public Shared Function SetHDData(ByVal pi As String, ByVal StartStopid As Integer, ByVal carHD() As CarWebServiceAdd.BoxHisView) As Boolean
        Dim strSql As String
        Dim tic As New TBCC.UtilityMobile.TranstrationItem
        strSql = String.Format("Insert Into TbccHistData_Box_{0}_1(ParentId,UpdateTime,AI1,AI2,AI3,AI4,Latitude_dir,Latitude,Longitude_dir,Longitude,AlarmStatus,fdapfinished,TransportState,histAlarmStorageType,gpsStorageType,histDataStorageType,unloadStorageType,histAlarmData) Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10,?p11,?p12,?p13,?p14,?p15,?p16,?p17);-- ", pi)

        For k As Integer = 0 To carHD.Length - 1
            Dim CarHDItem As CarWebServiceAdd.BoxHisView
            CarHDItem = carHD(k)

            If CarHDItem.updateTime = Nothing Then
                Exit Function
            End If
            Dim p(17) As MySql.Data.MySqlClient.MySqlParameter
            For i As Integer = 0 To p.GetUpperBound(0)
                p(i) = New MySql.Data.MySqlClient.MySqlParameter
                p(i).ParameterName = "p" + i.ToString
            Next
            p(0).Value = StartStopid
            p(1).Value = CarHDItem.updateTime
            p(2).Value = CarHDItem.ai1
            p(3).Value = -300
            p(4).Value = -300
            p(5).Value = -300
            p(6).Value = 0
            p(7).Value = 0
            p(8).Value = 0
            p(9).Value = 0
            p(10).Value = CarHDItem.alarmStatus
            p(11).Value = 0
            p(12).Value = 0
            p(13).Value = 0
            p(14).Value = 0
            p(15).Value = 0
            p(16).Value = 0
            p(17).Value = 0

            tic.Add(strSql & k, p)

        Next


        Return SqlHelper.ExecuteTranstration(tic)
    End Function

    Public Shared Function UpdateFDAPState(ByVal pi As String, ByVal StartStopid As Integer, ByVal update As DateTime) As Boolean

        Dim strSql As String
        Dim tic As New TBCC.UtilityMobile.TranstrationItem
        strSql = String.Format("update  TbccHistData_Box_{0}_1 set Fdapfinished=?p0 where ParentId=?p1 and UpdateTime=?p2", pi)

        Dim p(2) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.GetUpperBound(0)
            p(i) = New MySql.Data.MySqlClient.MySqlParameter
            p(i).ParameterName = "p" + i.ToString
        Next
        p(0).Value = 1
        p(1).Value = StartStopid
        p(2).Value = update


        tic.Add(strSql, p)

        Return SqlHelper.ExecuteTranstration(tic)

    End Function
End Class
